package com.praveen.twu.managers;

import com.praveen.twu.item.Movie;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by praveen on 20/6/14.
 */
public class MovieManager {
    private static List<Movie> movies = new ArrayList<Movie>();
    private static List<Movie> checkedout = new ArrayList<Movie>();

    public static List<Movie> getAvailableMovieList() {
        return movies;
    }

    public static void populate() {
        movies.clear();
        movies.add(new Movie("The Da Vinci Code", "Ron Howard", 2006, 7));
        movies.add(new Movie("The Dark Knight", "Christopher Nolan", 2008, 10));
        movies.add(new Movie("Sivaji", "Shankar", 2006, 8));
    }

    public static boolean movieAvailable(String name) {
        for (Movie b : movies)
            if (b.getName().equals(name))
                return true;
        return false;
    }

    private static Movie switchState(List<Movie> from, List<Movie> to, String name) {
        for (Movie b : from)
            if (b.getName().equals(name)) {
                to.add(b);
                from.remove(b);
                return b;
            }
        return null;
    }

    public static Movie checkout(String id) {
        if (!movieAvailable(id) || !LoginManager.isLoggedIn())
            return null;
        return switchState(movies, checkedout, id);
    }

    public static Movie returnMovie(String id) {
        if (movieAvailable(id) || !LoginManager.isLoggedIn())
            return null;
        return switchState(checkedout, movies, id);
    }
}